package cn.socialcredits.activity.data.implicits

import java.time.{Instant, LocalDateTime, ZoneOffset}

import cn.socialcredits.activity.data.model.{Activity, ActivityRegistration}
import reactivemongo.bson.{BSONDateTime, BSONHandler, Macros}

trait BSONImplicits {

  implicit object LocalDateTimeHandler extends BSONHandler[BSONDateTime, LocalDateTime] {
    override def read(bson: BSONDateTime): LocalDateTime =
      LocalDateTime.ofInstant(Instant.ofEpochMilli(bson.value), ZoneOffset.ofHours(8))

    override def write(t: LocalDateTime): BSONDateTime =
      BSONDateTime(t.toInstant(ZoneOffset.ofHours(8)).toEpochMilli)
  }

  implicit val __activityHandler = Macros.handler[Activity]
  implicit val __activityRegistrationHandler = Macros.handler[ActivityRegistration]
}

object BSONImplicits extends BSONImplicits
